package com.longdaji.decoration.ui.shippingaddr;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.data.bean.AddressBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by longtao on 2018/1/31.
 */

public class AddrsAdapter extends RecyclerView.Adapter<AddrsAdapter.ViewHolder> {

    private final AddrItemListener mListener;
    private Context mContext;
    private List<AddressBean.Address> mAddrList = new ArrayList<>();

    public interface AddrItemListener {

        void onEditAddrItem(AddressBean.Address addrItem);

        void onDeleteAddrItem(AddressBean.Address addrItem);

        void setDefaultAddr(AddressBean.Address addrItem);
    }

    public AddrsAdapter(Context context, List<AddressBean.Address> list, AddrItemListener listener) {
        if (list != null) {
            this.mAddrList = list;
        }
        this.mContext = context;
        this.mListener = listener;
    }


    public void setData(List<AddressBean.Address> list) {
        if (list != null) {
            this.mAddrList = list;
        }
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_addr, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final AddressBean.Address addrItem = mAddrList.get(position);
        if (addrItem.getContactPerson() != null) {
            holder.name.setText(addrItem.getContactPerson());
        }

        if (addrItem.getContactTelephone() != null) {
            holder.phone.setText(addrItem.getContactTelephone());
        }
        if (addrItem.getDetailAddress() != null && addrItem.getSimpleAddress() != null) {
            holder.detailAddr.setText(addrItem.getSimpleAddress() + addrItem.getDetailAddress());
        }
        if (addrItem.getDefaultAddress() != null) {
            if (TextUtils.equals("1", addrItem.getDefaultAddress())) {
                holder.isDefault.setChecked(true);
                holder.isDefault.setText("默认地址");
                holder.isDefault.setTextColor(ContextCompat.getColor(mContext, R.color.red));
            } else {
                holder.isDefault.setChecked(false);
                holder.isDefault.setText("设为默认");
                holder.isDefault.setTextColor(ContextCompat.getColor(mContext, R.color.important_txt_color));
            }

        }
        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onEditAddrItem(addrItem);
                }

            }
        });
        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onDeleteAddrItem(addrItem);
                }

            }
        });

        holder.isDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    if (TextUtils.equals("0", addrItem.getDefaultAddress())) {
                        mListener.setDefaultAddr(addrItem);
                    } else {
                        holder.isDefault.setChecked(true);
                    }
                }

            }
        });
        holder.addrItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.onEditAddrItem(addrItem);
                }


            }
        });


    }

    @Override
    public int getItemCount() {
        return mAddrList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name;
        private TextView phone;
        private TextView detailAddr;
        private TextView edit;
        private TextView delete;
        private LinearLayout addrItem;
        private CheckBox isDefault;

        public ViewHolder(View itemView) {
            super(itemView);
            addrItem = itemView.findViewById(R.id.llyt_addr_item);
            name = itemView.findViewById(R.id.tv_name);
            phone = itemView.findViewById(R.id.tv_phone);
            detailAddr = itemView.findViewById(R.id.tv_detail_addr);
            edit = itemView.findViewById(R.id.tv_edit);
            delete = itemView.findViewById(R.id.tv_delete);
            isDefault = itemView.findViewById(R.id.cd_is_default);
        }
    }

}
